﻿package b3.widget.card
{
	import b3.config.*;
	import b3.model.CardModel;
	import b3.model.ModelEvent;
	import b3.net.ResourceHelper;
	import b3.scene.home.*;
	import b3.util.*;
	import b3.widget.WidgetManager;
	import b3.widget.basic.PopMessage;
	
	import flash.display.Sprite;
	import flash.geom.*;
	
	public class CardItem extends Sprite
	{
		public var cardModel:CardModel;
		
		public function CardItem(cardModel:CardModel)
		{
			this.cardModel = cardModel;
			var cardConfig:CardConfig = Config.objConfigs[cardModel.objectIndex];
			var CardImg:Class = ResourceHelper.getInstance().getClassByName(cardConfig.iconImage[0]); 
			var cardImg:Sprite = new CardImg();
			addChild(cardImg);
			this.visible = false;
			this.mouseChildren = false;
			this.mouseEnabled = false;
			cardModel.addEventListener(ModelEvent.CARD_USE_FAIL, onCardUseFail);
			cardModel.addEventListener(ModelEvent.CARD_USE_SUCCESS, onCardUseSuccess);
			cardModel.addEventListener(ModelEvent.CARD_EXCLUSION, onCardExclusion);
		}
		
		private function onCardUseSuccess(event:ModelEvent):void
		{
			var home:HomeScene = HomeScene.getInstance(); 
			var cardConfig:CardConfig = Config.objConfigs[cardModel.objectIndex];
			switch (cardModel.objectIndex)
			{
				case CardConfig.VITALITY_SMALL:
				case CardConfig.VITALITY_BIG:
					home.vitalityPane.playAddMovie(new Point(BFont.stageWidth / 2, BFont.stageHeight / 3), cardConfig.effectValue);
				break;
				case CardConfig.TAX_INSPECT:
					var tax:int = event.data['tax'];
					home.goldCoinPane.playAddMovie(new Point(BFont.stageWidth / 2, BFont.stageHeight / 3), tax);
				break;
			}
			var popMessage:PopMessage = new PopMessage(cardConfig.name + " 使用成功！", 0x00AA00);
			WidgetManager.getInstance().popOut([popMessage]);
			home.setOperationMode(HomeScene.NONEMODE);
		}
		
		private function onCardExclusion( event:ModelEvent ):void
		{
			var popMessage:PopMessage = new PopMessage("已有同类型卡片被使用！", 0xFF0000);
			WidgetManager.getInstance().popOut([popMessage]);
			var home:HomeScene = HomeScene.getInstance(); 
			home.setOperationMode(HomeScene.NONEMODE);
		}
		
		private function onCardUseFail(event:ModelEvent):void
		{
			var cardConfig:CardConfig = Config.objConfigs[cardModel.objectIndex];
			var popMessage:PopMessage;
			switch (event.data['err'])
			{
				case 821:
					popMessage = new PopMessage("保护卡作用效力之下！", 0xFF0000);
					WidgetManager.getInstance().popOut([popMessage]);
				break;
				case 101:
					popMessage = new PopMessage("ta 没有足够金币支付税款！", 0xFF0000);
					WidgetManager.getInstance().popOut([popMessage]);
				break;
				case 102:
					popMessage = new PopMessage("不能对自己使用 " + cardConfig.name + " ！", 0xFF0000);
					WidgetManager.getInstance().popOut([popMessage]);
				break;
				case 103:
					popMessage = new PopMessage("不能对奇迹使用 " + cardConfig.name + " ！", 0xFF0000);
					WidgetManager.getInstance().popOut([popMessage]);
				break;
			}
			var home:HomeScene = HomeScene.getInstance(); 
			home.setOperationMode(HomeScene.NONEMODE);
		}
		
	}
}